package doc2db.interfaces.xmledit;

import doc2db.Keey;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import org.opu.stylepane.Ostermiller.Syntax.HighlightedDocument;
import org.opu.translator.gui.CheckPatternFrame;
import org.opu.xml.TestXml;

/**
 *
 * @author yura
 */
public class XmlEditorFrame extends JDialog{

    private static JDialog frame = new CheckPatternFrame(null);

    private XmlEditorMenu menu = new XmlEditorMenu(this);
    private XmlEditorTolBar bar;

    private Font paneFont = UserProp.paneFont;
    private Font textFont = UserProp.textFont;
    private String iconName [] = new String [] {
        FileBeginer.resurse_resurseFolderMenu+Keey.sp+"mark2.png", //tableIcon
        FileBeginer.resurse_resurseFolderMenu+Keey.sp+"createTempl.png"};//frameIcon


    private JTabbedPane jtp = new JTabbedPane();
    public MTextArea text1 = new MTextArea(HighlightedDocument.HTML_KEY_STYLE);
    public MTextArea text2 = new MTextArea(HighlightedDocument.HTML_KEY_STYLE);
    public MTextArea textAbout = new MTextArea(HighlightedDocument.PROPERTIES_STYLE);

    public XmlEditorFrame (String text1, String text2, boolean isHelp) {
        super(new Frame(), true);
        this.initComponent(text1, text2, isHelp);
        this.initFrame();
        this.addAllComponent();
    }

    private void addAllComponent() {
        Container contentPane = getContentPane();
        contentPane.add(jtp, BorderLayout.CENTER);
        contentPane.add(bar, BorderLayout.NORTH);
    }

    private void initComponent(String t1, String t2, boolean isHelp) {
        ImageIcon imageIcon = new ImageIcon(iconName[0]);
        jtp.addTab("templetes",imageIcon, new JScrollPane(text1), "шаблон");
        jtp.addTab("metaTempletes",imageIcon, new JScrollPane(text2), "шаблон шаблона");
        if (isHelp) {
            jtp.addTab("about",imageIcon, new JScrollPane(textAbout), "описание шаблона");
        }

        text1.setFont(textFont);
        text2.setFont(textFont);
        textAbout.setFont(textFont);
        text1.setText(t1);
        text2.setText(t2);
        textAbout.setText("Описание\n" +
                "Дата создания : " + new Date().toString() + " \n" +
                "Дата последнего исправления : " + new Date().toString() + " \n" +
                "Документы описываемые данным шаблоном : \n" +
                "Краткое описание шаблона : \n");
    }

    private void initFrame() {
        setTitle("editor");
        setSize(700, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        try {
            setIconImage(ImageIO.read(new File(iconName[1])));
        } catch (IOException e) {
        }
        setFont(paneFont);
        
        ActionListener checkXml = new CheckXml();
        ActionListener testLex = new CheckPatternAction();

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menuBar.add(menu.getMenu());
        menuBar.add(menu.getCorrect(checkXml, testLex));
        menuBar.add(menu.getHelp());

        bar = new XmlEditorTolBar(checkXml, testLex, menu.getHelpAction());
    }

    public void setSaveAction(ActionListener a){
        final ActionListener fa = a;
        ActionListener sa = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                CheckXml check = new CheckXml();
                if (check.isItOk()){
                    fa.actionPerformed(ae);
                }
            }
        };

        menu.addSaveAction(sa);
        bar.addSaveAction(sa);
    }

    class CheckXml implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent ae) {
            boolean itsOk = isItOk();

            if (itsOk) {
                JOptionPane.showMessageDialog(null, "XML дерево построено",
                        "no error", JOptionPane.INFORMATION_MESSAGE);
            }
        }

        public boolean isItOk() throws HeadlessException {
            boolean itsOk = true;
            String templateTxt = text1.getText();
            String metaTmpltTxt = text2.getText();
            
            TestXml tester = new TestXml(templateTxt);
            
            if (!tester.test()) {
                JOptionPane.showMessageDialog(null, "Ошибка в шаблоне:\n" 
                        + getMsg(tester), "template error", 0);
                itsOk = false;
            }
            
            tester.setText(metaTmpltTxt);
            if (!tester.test()) {
                JOptionPane.showMessageDialog(null, "Ошибка в мета-шаблоне:\n" 
                        + getMsg(tester), "meta-template error", 0);
                itsOk = false;
            }
            
            return itsOk;
        }

        private String getMsg(TestXml t){
            return String.valueOf(t.getErrorMsg()).replaceAll(";", ";\n");
        }

    }

    class CheckPatternAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            frame.dispose();
            frame = new CheckPatternFrame(XmlEditorFrame.this);
            frame.setVisible(true);
        }
    }


}
